package com.jinglvhuanjing.app.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;


public class ParamHelper {

    public static boolean insertOrUpdate(@NonNull Context context, @NonNull DBParamBean dbParamBean) {
        DBParamBean paramBean = querySingle(context, dbParamBean.getPid());
        if (paramBean == null) {
            ContentValues contentValues = DBParamTable.parse(dbParamBean);
            return SqlLiteHelper.getInstance(context).getSqLiteDatabase().
                    insert(DBParamTable.DB_TABLE_PARAM, null, contentValues) > 0;
        } else {
            final String condition = String.format("%s=?", DBParamTable.DB_PARAM_PID);
            final String[] values = new String[]{String.valueOf(paramBean.getPid())};
            ContentValues contentValues = DBParamTable.parse(paramBean);
            return SqlLiteHelper.getInstance(context).getSqLiteDatabase().
                    update(DBParamTable.DB_TABLE_PARAM, contentValues, condition, values) > 0;
        }
    }

    public static boolean insert(@NonNull Context context, @NonNull DBParamBean dbParamBean) {
        ContentValues contentValues = DBParamTable.parse(dbParamBean);
        return SqlLiteHelper.getInstance(context).getSqLiteDatabase().
                insert(DBParamTable.DB_TABLE_PARAM, null, contentValues) > 0;
    }


    public static DBParamBean querySingle(@NonNull Context context, String pid) {
        final String condition = String.format("%s=?", DBParamTable.DB_PARAM_PID);
        Cursor cursor = SqlLiteHelper.getInstance(context).getSqLiteDatabase().query(DBParamTable.DB_TABLE_PARAM,
                null, condition, new String[]{pid}, null, null, null);
        List<DBParamBean> list = new ArrayList<>();
        if (null != cursor) {
            while (cursor.moveToNext()) {
                list.add(DBParamTable.parse(cursor));
            }
            closeCursor(cursor);
        }
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public static List<DBParamBean> querySingle(@NonNull Context context) {
        Cursor cursor = SqlLiteHelper.getInstance(context).getSqLiteDatabase().query(DBParamTable.DB_TABLE_PARAM,
                null, null, null, null, null, null);
        List<DBParamBean> list = new ArrayList<>();
        if (null != cursor) {
            while (cursor.moveToNext()) {
                list.add(DBParamTable.parse(cursor));
            }
            closeCursor(cursor);
        }
        if (list.isEmpty()) {
            return null;
        } else {
            return list;
        }
    }


    public static boolean delete(@NonNull Context context, String pid) {
        String condition = String.format("%s=?", DBParamTable.DB_PARAM_PID);
        String[] values = new String[]{pid};
        return SqlLiteHelper.getInstance(context).getSqLiteDatabase()
                .delete(DBParamTable.DB_TABLE_PARAM, condition, values) > 0;
    }


    public static boolean delete(@NonNull Context context) {
        return SqlLiteHelper.getInstance(context).getSqLiteDatabase().
                delete(DBParamTable.DB_TABLE_PARAM, null, null) > 0;
    }

    private static void closeCursor(Cursor cursor) {
        if (null != cursor) {
            cursor.close();
        }
    }
}